import random
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response
from utils.common.tencent_sms import TencentSmsCode


class TencentSmsCodeAPIView(APIView):
    """腾讯短信发送(基于v3版本)"""
    authentication_classes = []

    def get(self, request, *args, **kwargs):
        telephone = request.GET.get('phone')
        tpl = request.GET.get('tpl')
        code = str(random.randrange(1000, 9999))
        # print(telephone, tpl, code)
        t_sms = TencentSmsCode(telephone, [code], tpl)
        # 普通短信发送函数调用
        # ret = t_sms.sms_check()
        # 异步短信发送函数调用
        ret = t_sms.sms_check_celery()
        if not ret:
            # print(ret)
            return Response(data='ok', status=status.HTTP_200_OK)
        data = {
            'status': ret.get("status"),
            'msg': ret.get("msg"),
        }
        # print(ret)
        return Response(data=data, status=status.HTTP_400_BAD_REQUEST)

